﻿''' <summary>
''' a card in the memory game
''' </summary>
''' <remarks></remarks>
Public Class Card

  ''' <summary>
  ''' constructor 
  ''' </summary>
  Public Sub New(ByVal imageUri As String)
    Me.uriOfImage = New Uri(imageUri)
  End Sub

  ''' <summary>
  ''' constructor
  ''' </summary>
  Public Sub New(ByVal imageUri As Uri)
    Me.uriOfImage = imageUri
  End Sub

  Private myTwinCard As Card
  ''' <summary>
  ''' card that is the twin card of this card
  ''' </summary>
  Public Property TwinCard() As Card
    Get
      Return Me.myTwinCard
    End Get
    Protected Set(ByVal value As Card)
      Me.myTwinCard = value
    End Set
  End Property

  ''' <summary>
  ''' creates the twin card. Is called by the Game after the cards are instantiated
  ''' </summary>
  ''' <remarks></remarks>
  Friend Sub CreateTwinCard()
    myTwinCard = New Card(uriOfImage)
    myTwinCard.TwinCard = Me
  End Sub

  Private uriOfImage As Uri
  ''' <summary>
  ''' uri to the image on the card
  ''' </summary>
  Public ReadOnly Property ImageUri() As Uri
    Get
      Return uriOfImage
    End Get
  End Property

  Public Enum Face
    Down
    Up
  End Enum

  Private cardFacing As Face = Face.Down
  ''' <summary>
  ''' indicates whether the card is facing up or down
  ''' </summary>
  Public ReadOnly Property Facing() As Face
    Get
      Return Me.cardFacing
    End Get
  End Property

  ''' <summary>
  ''' fires when the card is turned
  ''' </summary>
  Public Event Turned(ByVal sender As Object, ByVal e As CardTurnedEventArgs)

  ''' <summary>
  ''' turn the card to face the other side
  ''' </summary>
  Public Sub Turn()
    If cardFacing = Face.Down Then
      cardFacing = Face.Up
    Else
      cardFacing = Face.Down
    End If
    'publish change
    RaiseEvent Turned(Me, New CardTurnedEventArgs(Facing))
  End Sub

  ''' <summary>
  ''' fires when card is taken from the board
  ''' </summary>
  Public Event TakenChanged(ByVal sender As Object, ByVal e As EventArgs)

  Private cardTaken As Boolean
  ''' <summary>
  ''' when a card is taken from the board, the property becomes true
  ''' </summary>
  Public ReadOnly Property Taken() As Boolean
    Get
      Return Me.cardTaken
    End Get
  End Property

  ''' <summary>
  ''' take a card from the board
  ''' </summary>
  Friend Sub TakeFromBoard()
    Me.cardTaken = True
    RaiseEvent TakenChanged(Me, New EventArgs)
  End Sub

End Class

''' <summary>
''' event args for the Card.Turned event.
''' </summary>
Public Class CardTurnedEventArgs
  Inherits EventArgs


  Private cardFacing As Card.Face
  Public ReadOnly Property Facing() As Card.Face
    Get
      Return Me.cardFacing
    End Get
  End Property

  Public Sub New(ByVal facing As Card.Face)
    Me.cardFacing = facing
  End Sub
End Class